package com.example.teacher_app;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.MutableLiveData;
import androidx.viewpager.widget.ViewPager;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.teacher_app.databinding.ActivityMainTeacherBinding;
import com.example.teacher_app.vm.home.TeacherHomeFragment;
import com.example.teacher_app.vm.teachermine.TeacherMineFragment;

import java.util.ArrayList;
import java.util.List;

public class TeacherMainActivity extends AppCompatActivity {

    private long lastTime = 0;
    private static final long KEY_DELAY_TIME = 2000;
    private ActivityMainTeacherBinding binding;
    private MutableLiveData<Fragment> fragmentLiveData = new MutableLiveData<>();
    private TchViewPagerAdapter tchViewPagerAdapter;
    private List<Fragment> mfragments = new ArrayList<Fragment>();

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(0xFF4682B4);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = ActivityMainTeacherBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        fragmentLiveData.observe(this, fragment -> {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (int i = 0; i < fragments.size(); i++) {
                fragmentTransaction.hide(fragments.get(i));
            }
            if (fragment.isAdded()) {
                fragmentTransaction.show(fragment).commitAllowingStateLoss();
            } else {
                fragmentTransaction.add(R.id.teacher_container, fragment).commitAllowingStateLoss();
            }
            Log.d("TAG", "onCreate: 页面切换" + fragment.getClass().getName());
        });

        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.teacher_schedule) {
                    binding.teacherViewpager.setCurrentItem(0,true);
                } else if (checkedId == R.id.teacher_mine) {
                    binding.teacherViewpager.setCurrentItem(1,true);
                }
            }
        });
        binding.teacherSchedule.setChecked(true);

        mfragments.clear();
        mfragments.add(TeacherHomeFragment.newInstance());
        mfragments.add(TeacherMineFragment.newInstance());

        tchViewPagerAdapter = new TchViewPagerAdapter(this.getSupportFragmentManager(),mfragments);
        binding.teacherViewpager.setOffscreenPageLimit(2);
        binding.teacherViewpager.setAdapter(tchViewPagerAdapter);
        binding.teacherViewpager.setCurrentItem(0);

        binding.teacherViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changeBottom(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void changeBottom(int position){
        if (position == 0){
            binding.teacherSchedule.setChecked(true);
        }else {
            binding.teacherMine.setChecked(true);
        }
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastTime < KEY_DELAY_TIME) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
            lastTime = System.currentTimeMillis();
        }
    }
}